package com.atguigu.java1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 实现TCP的网络编程
 * 例题3：从客户端发送文件给服务端，服务端保存在本地，并返回“发送成功”给及客户端，
 * 并关闭相应的连接
 *
 *
 */
public class TCPTest3 {

    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1.创建Socket接口，并指明IP地址和端口号
            socket = new Socket(InetAddress.getByName("100.78.71.245"),8898);
            //2.获取输出流
            os = socket.getOutputStream();
            //3.写出输出的操作
           bis = new BufferedInputStream(new FileInputStream(new File("beauty.jpg")));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                os.write(buffer,0,len);
            }
            //关闭数据的输出
            socket.shutdownOutput();

            //5.接受来自服务器的数据，并显示到控制台上
            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[20];
            int len1;
            while((len1 = is.read(buffer1)) != -1){
                baos.write(buffer1,0,len1);
            }

            System.out.println(baos.toString());


        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //4.关闭流
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is1 = null;
        BufferedOutputStream bos = null;
        OutputStream os = null;
        try {
            ss = new ServerSocket(8898);

            socket = ss.accept();

            is1 = socket.getInputStream();

            bos = new BufferedOutputStream(new FileOutputStream(new File("beauty2.jpg")));
            byte[] buffer = new byte[1024];
            int len;
            while((len = is1.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }

            System.out.println("图片传输完成");
            //6.服务器给予客户端反馈
            os = socket.getOutputStream();
            os.write("你好，我已经收到！".getBytes());

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(bos != null) {
                try {
                   bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(is1 != null) {
                try {
                    is1.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
